package model;

import app.App;

import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StockOut {

    private int stockOutId;
    private int productId;
    private String productName;
    private float sellingPrice;
    private float purchasePrice;
    private int count;
    private String remark;
    private Timestamp stockOutTime;
    private Product product;

    public int getStockOutId() {
        return stockOutId;
    }

    public void setStockOutId(int stockOutId) {
        this.stockOutId = stockOutId;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public float getSellingPrice() {
        return sellingPrice;
    }

    public void setSellingPrice(float sellingPrice) {
        this.sellingPrice = sellingPrice;
    }

    public float getPurchasePrice() {
        return purchasePrice;
    }

    public void setPurchasePrice(float purchasePrice) {
        this.purchasePrice = purchasePrice;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Timestamp getStockOutTime() {
        return stockOutTime;
    }

    public void setStockOutTime(Timestamp stockOutTime) {
        this.stockOutTime = stockOutTime;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Object[] toTableObjectArray() {
        if(sellingPrice == .0){//退货
            return new Object[]{stockOutId, productId, productName, "", purchasePrice, count, remark};
        }
        else {//销售
            return new Object[]{stockOutId, productId, productName, sellingPrice, purchasePrice, count, remark};
        }
    }

    @Override
    public String toString() {
        return "StockOut{" +
                "stockOutId=" + stockOutId +
                ", productid=" + productId +
                ", productName='" + productName + '\'' +
                ", sellingPrice=" + sellingPrice +
                ", purchasePrice=" + purchasePrice +
                ", count=" + count +
                ", remark='" + remark + '\'' +
                ", product=" + product +
                '}';
    }

    //获取出库列表
    public static List<StockOut> getStockOutList() {
        List<StockOut> stockOutList = new ArrayList<>();

        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM stockout");
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                StockOut stockOut = new StockOut();
                stockOut.setStockOutId(rs.getInt(1));
                stockOut.setProductId(rs.getInt(2));
                stockOut.setProductName(rs.getString(3));
                stockOut.setSellingPrice(rs.getFloat(4));
                stockOut.setPurchasePrice(rs.getFloat(5));
                stockOut.setCount(rs.getInt(6));
                stockOut.setRemark(rs.getString(7));
                stockOut.setStockOutTime(rs.getTimestamp(8));
                stockOutList.add(stockOut);
                System.out.println(stockOut);
            }
            conn.close();
            return stockOutList;
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    //退货
    public static boolean returnProduct(int stockInId) {
        Connection conn = null;
        try {
            conn = App.dataSource.getConnection();//打开连接
            conn.setAutoCommit(false);

            StockIn stockIn = StockIn.getStockInRecord(stockInId);

            if (stockIn != null) {
                //减少库存
                PreparedStatement ps_modCount = conn.prepareStatement(
                        "UPDATE `product` SET `quantity` = `quantity` - ? WHERE (`productid` = ?)");
                ps_modCount.setObject(1, stockIn.getCount());
                ps_modCount.setObject(2, stockIn.getProductId());
                ps_modCount.executeUpdate();

                //减少库存
                PreparedStatement ps_delIn = conn.prepareStatement(
                        "DELETE FROM `stockin` WHERE (`stockinid` = ?)");
                ps_delIn.setObject(1, stockIn.getStockInId());
                ps_delIn.executeUpdate();

                //记录退货
                PreparedStatement ps_addOut = conn.prepareStatement(
                        "INSERT INTO `stockout` (`stockoutid`, `productid`, `productname`, `purchaseprice`, `count`, `remark`, `stockouttime`) " +
                                "VALUES (?, ?, ?, ?, ?, ?, ?)");
                ps_addOut.setObject(1, StockOut.getNewStockOutId());
                ps_addOut.setObject(2, stockIn.getProductId());
                ps_addOut.setObject(3, stockIn.getProductName());
                ps_addOut.setObject(4, stockIn.getPurchasePrice());
                ps_addOut.setObject(5, stockIn.getCount());
                ps_addOut.setObject(6, "退货至"+stockIn.getVendor());
                ps_addOut.setObject(7, new Timestamp(new Date().getTime()));
                ps_addOut.executeUpdate();

                conn.commit();
                conn.close();
                System.out.println("退货成功");
                return true;
            }
            System.out.println("入库记录不存在");
        } catch (SQLException throwable) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            throwable.printStackTrace();
        }
        return false;
    }

    //获取一个新的入库号
    public static int getNewStockOutId() {
        Connection conn;
        try {
            conn = App.dataSource.getConnection();//打开连接
            PreparedStatement ps = conn.prepareStatement("SELECT MAX(stockoutid) AS max FROM stockout");
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                int max = rs.getInt("max") + 1;
                conn.close();
                return max;
            }
            conn.close();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return 1;
    }
}
